﻿//////////////////////////////
//Utility.
//Uncategorized snippet functions.
//////////////////////////////
package org.bbdesign.airbuzz {
	import flash.display.*;
	import flash.events.*;
	import flash.net.*;
	import flash.geom.*;
	import flash.filters.*;
	import flash.utils.*;
	import flash.text.*;
	//////////////////////////////
	public class Utility extends Sprite {
		//////////////////////////////
		//Internal class variables.
		//////////////////////////////
		//Instance.
		private var _webPageURL:URLRequest = new URLRequest();
		private var _jscommand:String;
		private var _glowfilter:GlowFilter = new GlowFilter();
		private var _dropShadow:DropShadowFilter = new DropShadowFilter();
		//////////////////////////////
		//Constructor.
		//////////////////////////////
		public function Utility() {
		}
		/////////////////////////////
		//Position debug.
		//////////////////////////////
		public function pointDebug(dispobject:DisplayObject):void {
			var clickPoint:Point = new Point(dispobject.mouseX, dispobject.mouseY);
			trace('Display object coordinates: ' + clickPoint);
			trace('Stage coordinates: ' + dispobject.localToGlobal(clickPoint));
		}
		/////////////////////////////
		//Add multiple children.
		//////////////////////////////
		public function addChildren (objarray:Array, container:Boolean = false, cont:Sprite = null):void {
			for (var j:int = 0; j < objarray.length; j++) {
				if(container) {
					cont.addChild(objarray[j]);
				} else {
					addChild (objarray[j]);
				}
			}
		}
		//////////////////////////////
		//Mouse event builder.
		//////////////////////////////
		public function mouseBuildHandler (dispatcher:MovieClip, evtypeops:Array, meventarg:Function):void {
			for (var i:int = 0; i < evtypeops.length; i++) {
				switch (evtypeops[i]) {
					case 'down' :
						dispatcher.addEventListener (MouseEvent.MOUSE_DOWN, meventarg);
					break;
					case 'up' :
						dispatcher.addEventListener (MouseEvent.MOUSE_UP, meventarg);
					break;
					case 'over' :
						dispatcher.addEventListener (MouseEvent.MOUSE_OVER, meventarg);
					break;
					case 'out' :
						dispatcher.addEventListener (MouseEvent.MOUSE_OUT, meventarg);
					break;
					case 'click' :
						dispatcher.addEventListener (MouseEvent.CLICK, meventarg);
					break;
				}
			}
		}
		//////////////////////////////
		//Enterframe handler.
		//////////////////////////////
		public function enterFrameHandler (entframefunc:Function, entframedestroy:String = undefined):void{
			addEventListener (Event.ENTER_FRAME, entFrame);
			function entFrame (e:Event):void {
				entframefunc();
				if (entframedestroy == 'destroy') {
					removeEventListener (Event.ENTER_FRAME, entFrame);
				}
			}
		}
		/////////////////////////////
		//Navigate to URL.
		//////////////////////////////
		public function goToURL (urlStr:String, targ:String, popup:Boolean):void {
			if(popup) {
				_jscommand = "window.open('" + urlStr + "','PopUpWindow','height=550,width=350,toolbar=no,scrollbars=no,resizable=yes');";
				_webPageURL = new URLRequest("javascript:" + _jscommand + " void(0);");
			} else {
				_webPageURL = new URLRequest(urlStr);
			}
			navigateToURL (_webPageURL, targ);
		}
		//////////////////////////////
		//Random number range.
		//////////////////////////////
		public function randRange (minNum:Number, maxNum:Number):Number {
			return Math.floor(Math.random() * maxNum - minNum + 1) + minNum;
		}
		//////////////////////////////
		//URL Loader builder.
		//Text/Binary & SWF/Image objects.
		//////////////////////////////
		public function urlLoaderBuilder (loadURL:String, loaderformat:String = 'text', loadertype:String = 'urlloader'):* {
			var req:URLRequest = new URLRequest(loadURL);
			req.method = URLRequestMethod.POST;
			switch(loadertype) {
				case 'urlloader':
					var urlloader:URLLoader = new URLLoader();
					urlloader.load (req);
					switch(loaderformat) {
					case 'text':
						urlloader.dataFormat = URLLoaderDataFormat.TEXT;
					break;
					case 'variables':
						urlloader.dataFormat = URLLoaderDataFormat.VARIABLES;
					break;
					case 'binary':
						urlloader.dataFormat = URLLoaderDataFormat.BINARY;
					break;
				}
				return urlloader;
				break;
				case 'loader':
					var loader:Loader = new Loader();
					loader.load (req);
					return loader;
				break;
			}
		}
		//////////////////////////////
		//Get length of object.
		//////////////////////////////
		public function getObjLength(o:Object):uint {
			var len:uint = 0;
			for (var item:* in o) {
				if (item != "mx_internal_uid") {
					len++;
				}
			}
			return len;
		}
		//////////////////////////////
		//Clone object.
		//////////////////////////////
		public function cloneObj(obj:Object):* {
			var copier:ByteArray = new ByteArray();
			copier.writeObject(obj);
			copier.position = 0;
			return(copier.readObject());
		}
		//////////////////////////////
		//Dynamic registration point.
		//////////////////////////////
		public function setRegistrationPoint(s:Sprite, regx:Number, regy:Number, showRegistration:Boolean):* {
			//translate movieclip
			s.transform.matrix = new Matrix(1, 0, 0, 1, -regx, -regy);
			//registration point.
			if (showRegistration) {
				var mark:Sprite = new Sprite();
				mark.graphics.lineStyle(1, 0x000000);
				mark.graphics.moveTo(-5, -5);
				mark.graphics.lineTo(5, 5);
				mark.graphics.moveTo(-5, 5);
				mark.graphics.lineTo(5, -5);
				s.parent.addChild(mark);
			}
		}
		//////////////////////////////
		//Remove duplicates from array.
		//////////////////////////////
		public function arrayRemDup(array:Array):Array {
			for(var a:int = array.length; a > 0; a--) {
				if (array[a] == array[a - 1]) {
					array.splice(a, 1);
				}
			}
			return array;
		}
		//////////////////////////////
		//Font glyph checking.
		//////////////////////////////
		public function checkFontGlyphs(txtBox:TextField, txt:String, desiredFont:String):void {
			txtBox.text = txt;
			var allFonts:Array = Font.enumerateFonts(false);
			for (var font:String in allFonts) {
				if (allFonts[font].fontName == desiredFont) {
					var format:TextFormat = new TextFormat();
					if (allFonts[font].hasGlyphs(txt)) {
						//trace('Embedded font.');
						format.font = desiredFont;
						txtBox.setTextFormat(format);
						txtBox.embedFonts = true;
					} else {
						//trace('System font.');
						format.font = "_sans";
						txtBox.setTextFormat(format);
						txtBox.embedFonts = false;
					}
				}
			}
		}
		//////////////////////////////
		//Initialize filters.
		//////////////////////////////
		public function initGlowFilter():GlowFilter {
			_glowfilter.color = 0x003366;
			_glowfilter.blurX = 12;
			_glowfilter.blurY = 12;
			_glowfilter.quality = 3;
			return _glowfilter;
		}
		public function initDropShadowFilter():DropShadowFilter {
			_dropShadow.color = 0x000000;
			_dropShadow.blurX = 5;
			_dropShadow.blurY = 5;
			_dropShadow.angle = 30;
			_dropShadow.alpha = 0.5;
			_dropShadow.distance = 5;
			_dropShadow.quality = 3;
			return _dropShadow;
		}
	}
}

